package com.ib.algokit.charting;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.SegmentedTimeline;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYDifferenceRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;


public class DifferenceChartDemo1 extends ApplicationFrame
{
	public DifferenceChartDemo1(String paramString)
	{
		super(paramString);
		JPanel localJPanel = createDemoPanel();
//		localJPanel.setPreferredSize(new Dimension(500, 270));
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		localJPanel.setPreferredSize(dim);
		setContentPane(localJPanel);
	}

	public DifferenceChartDemo1(String paramString, XYDataset paramXYDataset)
	{
		super(paramString);
		JPanel localJPanel = createPanel(paramString, paramXYDataset);
		Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
		localJPanel.setPreferredSize(dim);
		setContentPane(localJPanel);
	}
	
	private static XYDataset createDataset()
	{
		TimeSeries localTimeSeries1 = new TimeSeries("Random 1");
		TimeSeries localTimeSeries2 = new TimeSeries("Random 2");
		double d1 = 0.0D;
		double d2 = 0.0D;
		Day localDay = new Day();
		for (int i = 0; i < 200; ++i)
		{
			d1 = d1 + Math.random() - 0.5D;
			d2 = d2 + Math.random() - 0.5D;
			localTimeSeries1.add(localDay, d1);
			localTimeSeries2.add(localDay, d2);
			localDay = (Day)localDay.next();
		}
		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
		localTimeSeriesCollection.addSeries(localTimeSeries1);
		localTimeSeriesCollection.addSeries(localTimeSeries2);
		return localTimeSeriesCollection;
	}

//	public static XYDataset createLongPercChangeRangesDataset(String pair, TreeMap<Interval<TimeStamp>,Properties> instrumentGenomePropertiesPerPeriod)
//	{
//		TimeSeries beginTimeSeries = new TimeSeries("Begin");
//		TimeSeries endTimeSeries = new TimeSeries("End");
//        double totalLongProjectedProfit=0;
//		for(Entry<Interval<TimeStamp>,Properties> value:instrumentGenomePropertiesPerPeriod.entrySet()){
//			Interval<TimeStamp> interval=value.getKey();
//			Properties properties=value.getValue();
//			Date from=interval.from.getDate();
//			Date to=interval.to.getDate();
//			Minute fromDay = new Minute(from);
//			Minute toDay = new Minute(to);
//			double longPercChangeBegin=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.LONG_PERC_CHANGE_BEGIN));
//			double longPercChangeEnd=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.LONG_PERC_CHANGE_END));
//			if(longPercChangeBegin!=-10&&longPercChangeEnd!=-10){
//				double longProjectedProfit=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.LONG_PROJECTED_PROFIT));
//				System.out.println(pair+": "+fromDay+"-"+toDay+" ["+longPercChangeBegin+" "+longPercChangeEnd+"] projProfit:"+longProjectedProfit);
//                totalLongProjectedProfit+=longProjectedProfit;
//            }
//			beginTimeSeries.add(fromDay, (longPercChangeBegin==-10?0:longPercChangeBegin));
//			beginTimeSeries.add(toDay, (longPercChangeBegin==-10?0:longPercChangeBegin));
//			
//			endTimeSeries.add(fromDay, (longPercChangeEnd==-10?0:longPercChangeEnd));
//			endTimeSeries.add(toDay, (longPercChangeEnd==-10?0:longPercChangeEnd));
//		}
//		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
//		System.out.println("totalLongProjectedProfit="+totalLongProjectedProfit);
//		localTimeSeriesCollection.addSeries(endTimeSeries);
//		localTimeSeriesCollection.addSeries(beginTimeSeries);
//		return localTimeSeriesCollection;
//	}
	
//	public static XYDataset createShortPercChangeRangesDataset(String pair, TreeMap<Interval<TimeStamp>,Properties> instrumentGenomePropertiesPerPeriod)
//	{
//		TimeSeries beginTimeSeries = new TimeSeries("Begin");
//		TimeSeries endTimeSeries = new TimeSeries("End");
//        double totalShortProjectedProfit=0;
//		for(Entry<Interval<TimeStamp>,Properties> value:instrumentGenomePropertiesPerPeriod.entrySet()){
//			Interval<TimeStamp> interval=value.getKey();
//			Properties properties=value.getValue();
//			Date from=interval.from.getDate();
//			Date to=interval.to.getDate();
//			Minute fromDay = new Minute(from);
//			Minute toDay = new Minute(to);
//			double shortPercChangeBegin=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.SHORT_PERC_CHANGE_BEGIN));
//			double shortPercChangeEnd=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.SHORT_PERC_CHANGE_END));
////			System.out.println(pair+": "+fromDay+"-"+toDay+" ["+shortPercChangeBegin+" "+shortPercChangeEnd+"]");
//			if(shortPercChangeBegin!=-10&&shortPercChangeEnd!=-10){
//				double shortProjectedProfit=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.SHORT_PROJECTED_PROFIT));
//				System.out.println(pair+": "+fromDay+"-"+toDay+" ["+shortPercChangeBegin+" "+shortPercChangeEnd+"] projProfit:"+shortProjectedProfit);
//                totalShortProjectedProfit+=shortProjectedProfit;
//            }			
//			beginTimeSeries.add(fromDay, (shortPercChangeBegin==-10?0:shortPercChangeBegin));
//			beginTimeSeries.add(toDay, (shortPercChangeBegin==-10?0:shortPercChangeBegin));
//			
//			endTimeSeries.add(fromDay, (shortPercChangeEnd==-10?0:shortPercChangeEnd));
//			endTimeSeries.add(toDay, (shortPercChangeEnd==-10?0:shortPercChangeEnd));
//		}
//		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
//		System.out.println("totalShortProjectedProfit="+totalShortProjectedProfit);
//		localTimeSeriesCollection.addSeries(endTimeSeries);
//		localTimeSeriesCollection.addSeries(beginTimeSeries);
//		return localTimeSeriesCollection;
//	}
	
//	public static XYDataset createLongAvgAltPercChangeRangesDataset(String pair, TreeMap<Interval<TimeStamp>,Properties> instrumentGenomePropertiesPerPeriod)
//	{
//		TimeSeries beginTimeSeries = new TimeSeries("Begin");
//		TimeSeries endTimeSeries = new TimeSeries("End");
//		for(Entry<Interval<TimeStamp>,Properties> value:instrumentGenomePropertiesPerPeriod.entrySet()){
//			Interval<TimeStamp> interval=value.getKey();
//			Properties properties=value.getValue();
//			Date from=interval.from.getDate();
//			Date to=interval.to.getDate();
//			Minute fromDay = new Minute(from);
//			Minute toDay = new Minute(to);
//			String longAvgAltPercChangeBeginProp=properties.getProperty(pair+"."+GenomeProperties.LONG_AVG_ALT_PERC_CHANGE_BEGIN);
//			double longAvgAltPercChangeBegin=longAvgAltPercChangeBeginProp==null?0:CSVFormat.EG_FORMAT.parse(longAvgAltPercChangeBeginProp);
//			String longAvgAltPercChangeEndProp=properties.getProperty(pair+"."+GenomeProperties.LONG_AVG_ALT_PERC_CHANGE_END);
//			double longAvgAltPercChangeEnd=longAvgAltPercChangeEndProp==null?0:CSVFormat.EG_FORMAT.parse(longAvgAltPercChangeEndProp);
//			System.out.println(pair+": "+fromDay+"-"+toDay+" ["+longAvgAltPercChangeBegin+" "+longAvgAltPercChangeEnd+"]");
//			beginTimeSeries.add(fromDay, longAvgAltPercChangeBegin);
//			beginTimeSeries.add(toDay, longAvgAltPercChangeBegin);
//			endTimeSeries.add(fromDay, longAvgAltPercChangeEnd);
//			endTimeSeries.add(toDay, longAvgAltPercChangeEnd);
//		}
//		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();		
//		localTimeSeriesCollection.addSeries(endTimeSeries);
//		localTimeSeriesCollection.addSeries(beginTimeSeries);
//		return localTimeSeriesCollection;
//	}
	
//	public static XYDataset createShortAvgAltPercChangeRangesDataset(String pair, TreeMap<Interval<TimeStamp>,Properties> instrumentGenomePropertiesPerPeriod)
//	{
//		TimeSeries beginTimeSeries = new TimeSeries("Begin");
//		TimeSeries endTimeSeries = new TimeSeries("End");
//		for(Entry<Interval<TimeStamp>,Properties> value:instrumentGenomePropertiesPerPeriod.entrySet()){
//			Interval<TimeStamp> interval=value.getKey();
//			Properties properties=value.getValue();
//			Date from=interval.from.getDate();
//			Date to=interval.to.getDate();
//			Minute fromDay = new Minute(from);
//			Minute toDay = new Minute(to);
//			String shortAvgAltPercChangeBeginProp=properties.getProperty(pair+"."+GenomeProperties.SHORT_AVG_ALT_PERC_CHANGE_BEGIN);
//			double shortAvgAltPercChangeBegin=shortAvgAltPercChangeBeginProp==null?0:CSVFormat.EG_FORMAT.parse(shortAvgAltPercChangeBeginProp);
//			String shortAvgAltPercChangeEndProp=properties.getProperty(pair+"."+GenomeProperties.SHORT_AVG_ALT_PERC_CHANGE_END);
//			double shortAvgAltPercChangeEnd=shortAvgAltPercChangeEndProp==null?0:CSVFormat.EG_FORMAT.parse(shortAvgAltPercChangeEndProp);
//			System.out.println(pair+": "+fromDay+"-"+toDay+" ["+shortAvgAltPercChangeBegin+" "+shortAvgAltPercChangeEnd+"]");
//			beginTimeSeries.add(fromDay, shortAvgAltPercChangeBegin);
//			beginTimeSeries.add(toDay, shortAvgAltPercChangeBegin);			
//			endTimeSeries.add(fromDay, shortAvgAltPercChangeEnd);
//			endTimeSeries.add(toDay, shortAvgAltPercChangeEnd);
//		}
//		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
//		
//		localTimeSeriesCollection.addSeries(endTimeSeries);
//		localTimeSeriesCollection.addSeries(beginTimeSeries);
//		return localTimeSeriesCollection;
//	}
	
//	public static XYDataset createLongProfitLossPipsRangesDataset(String pair, TreeMap<Interval<TimeStamp>,Properties> instrumentGenomePropertiesPerPeriod)
//	{
//		TimeSeries beginTimeSeries = new TimeSeries("Begin");
//		TimeSeries endTimeSeries = new TimeSeries("End");
//		double totalLongProjectedProfit=0;
//		for(Entry<Interval<TimeStamp>,Properties> value:instrumentGenomePropertiesPerPeriod.entrySet()){
//			Interval<TimeStamp> interval=value.getKey();
//			Properties properties=value.getValue();
//			Date from=interval.from.getDate();
//			Date to=interval.to.getDate();
//			Minute fromDay = new Minute(from);
//			Minute toDay = new Minute(to);
//
//			String longLimitProfitPipsProp=properties.getProperty(pair+"."+GenomeProperties.LONG_LIMIT_PROFIT_PIPS);
//			double longLimitProfitPips=longLimitProfitPipsProp==null?0:CSVFormat.EG_FORMAT.parse(longLimitProfitPipsProp);
//			String longStopLossPipsProp=properties.getProperty(pair+"."+GenomeProperties.LONG_STOP_LOSS_PIPS);
//			double longStopLossPips=longStopLossPipsProp==null?0:CSVFormat.EG_FORMAT.parse(longStopLossPipsProp);
//
//			if(longLimitProfitPips!=0&&longStopLossPips!=0){
//				double longProjectedProfit=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.LONG_PROJECTED_PROFIT));
//				double longMaxLimitWaitTime=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.LONG_MAX_LIMIT_WAIT_TIME));
//				System.out.println(pair+": "+fromDay+"-"+toDay+" [ p:"+longLimitProfitPips+" l:"+longStopLossPips+" maxWait:"+longMaxLimitWaitTime+"] projProfit:"+longProjectedProfit);
//				totalLongProjectedProfit+=longProjectedProfit;
//			}
//			beginTimeSeries.add(fromDay, longLimitProfitPips);
//			beginTimeSeries.add(toDay, longLimitProfitPips);
//
//			endTimeSeries.add(fromDay, longStopLossPips);
//			endTimeSeries.add(toDay, longStopLossPips);
//		}
//		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
//		System.out.println("totalLongProjectedProfit="+totalLongProjectedProfit);
//		localTimeSeriesCollection.addSeries(endTimeSeries);
//		localTimeSeriesCollection.addSeries(beginTimeSeries);
//		return localTimeSeriesCollection;
//	}

//	public static XYDataset createShortProfitLossPipsRangesDataset(String pair, TreeMap<Interval<TimeStamp>,Properties> instrumentGenomePropertiesPerPeriod)
//	{
//		TimeSeries beginTimeSeries = new TimeSeries("Begin");
//		TimeSeries endTimeSeries = new TimeSeries("End");
//		double totalShortProjectedProfit=0;
//		for(Entry<Interval<TimeStamp>,Properties> value:instrumentGenomePropertiesPerPeriod.entrySet()){
//			Interval<TimeStamp> interval=value.getKey();
//			Properties properties=value.getValue();
//			Date from=interval.from.getDate();
//			Date to=interval.to.getDate();
//			Minute fromDay = new Minute(from);
//			Minute toDay = new Minute(to);
//
//			String shortLimitProfitPipsProp=properties.getProperty(pair+"."+GenomeProperties.SHORT_LIMIT_PROFIT_PIPS);
//			double shortLimitProfitPips=shortLimitProfitPipsProp==null?0:CSVFormat.EG_FORMAT.parse(shortLimitProfitPipsProp);
//			String shortStopLossPipsProp=properties.getProperty(pair+"."+GenomeProperties.SHORT_STOP_LOSS_PIPS);
//			double shortStopLossPips=shortStopLossPipsProp==null?0:CSVFormat.EG_FORMAT.parse(shortStopLossPipsProp);
//			if(shortLimitProfitPips!=0&&shortStopLossPips!=0){
//				double shortProjectedProfit=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.SHORT_PROJECTED_PROFIT));
//				double shortMaxLimitWaitTime=CSVFormat.EG_FORMAT.parse(properties.getProperty(pair+"."+GenomeProperties.SHORT_MAX_LIMIT_WAIT_TIME));
//				System.out.println(pair+": "+fromDay+"-"+toDay+" [ p:"+shortLimitProfitPips+" l:"+shortStopLossPips+" maxWait:"+shortMaxLimitWaitTime+"] projProfit:"+shortProjectedProfit);
//				totalShortProjectedProfit+=shortProjectedProfit;
//			}
//			beginTimeSeries.add(fromDay, shortLimitProfitPips);
//			beginTimeSeries.add(toDay, shortLimitProfitPips);
//
//			endTimeSeries.add(fromDay, shortStopLossPips);
//			endTimeSeries.add(toDay, shortStopLossPips);
//		}
//		TimeSeriesCollection localTimeSeriesCollection = new TimeSeriesCollection();
//		System.out.println("totalShortProjectedProfit="+totalShortProjectedProfit);
//		localTimeSeriesCollection.addSeries(endTimeSeries);
//		localTimeSeriesCollection.addSeries(beginTimeSeries);
//		return localTimeSeriesCollection;
//	}
	
	private static JFreeChart createChart(String frameTitle,XYDataset paramXYDataset)
	{
		JFreeChart localJFreeChart = ChartFactory.createTimeSeriesChart(frameTitle/*"Difference Chart Demo 1"*/, "Time", "Value", paramXYDataset, true, true, false);
		XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot();
		localXYPlot.setDomainPannable(true);
		XYDifferenceRenderer localXYDifferenceRenderer = new XYDifferenceRenderer(Color.green, Color.red, false);
		localXYDifferenceRenderer.setRoundXCoordinates(false);
		localXYPlot.setDomainCrosshairLockedOnData(true);
		localXYPlot.setRangeCrosshairLockedOnData(true);
		localXYPlot.setDomainCrosshairVisible(true);
		localXYPlot.setRangeCrosshairVisible(true);
		localXYPlot.setRenderer(localXYDifferenceRenderer);
		DateAxis localDateAxis = new DateAxis("Time");
		localDateAxis.setLowerMargin(0.0D);
		localDateAxis.setUpperMargin(0.0D);
		
		//Mon-Fri 9:30 - 17 axis
//		SegmentedTimeline mondayThroughFridayTimeline=SegmentedTimeline.newMondayThroughFridayTimeline();
		SegmentedTimeline timeline = new SegmentedTimeline(SegmentedTimeline.FIFTEEN_MINUTE_SEGMENT_SIZE, 30, 66);
        timeline.setStartTime(SegmentedTimeline.firstMondayAfter1900() + (38*timeline.getSegmentSize()) );
        timeline.setBaseTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());

//		localDateAxis.setTimeline(timeline);

        
		localXYPlot.setDomainAxis(localDateAxis);
		
		NumberAxis yAxis= (NumberAxis)localXYPlot.getRangeAxis();
		yAxis.setRange(-0.001, 0.001);
		localXYPlot.setForegroundAlpha(0.5F);
		ChartUtilities.applyCurrentTheme(localJFreeChart);
//		ChartUtilities.w
		return localJFreeChart;
	}

	public static JPanel createDemoPanel()
	{
		JFreeChart localJFreeChart = createChart(null,createDataset());
		ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
		localChartPanel.setMouseWheelEnabled(true);
		return localChartPanel;
	}
	
	public static JPanel createPanel(String frameTitle, XYDataset paramXYDataset)
	{
		JFreeChart localJFreeChart = createChart(frameTitle, paramXYDataset);
		ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
		localChartPanel.setMouseWheelEnabled(true);
		return localChartPanel;
	}

	public static void main(String[] paramArrayOfString)
	{
		DifferenceChartDemo1 localDifferenceChartDemo1 = new DifferenceChartDemo1("JFreeChart: DifferenceChartDemo1.java");
		localDifferenceChartDemo1.pack();
		RefineryUtilities.centerFrameOnScreen(localDifferenceChartDemo1);
		localDifferenceChartDemo1.setVisible(true);
	}
	
	public static void showChart(String frameTitle, XYDataset paramXYDataset) 
	{
		DifferenceChartDemo1 localDifferenceChartDemo1 = new DifferenceChartDemo1(frameTitle, paramXYDataset);
		localDifferenceChartDemo1.pack();
		RefineryUtilities.centerFrameOnScreen(localDifferenceChartDemo1);
		localDifferenceChartDemo1.setVisible(true);
	}
}
